package com.evl.vehicleSchedule.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.evl.common.controller.BaseController;
import com.evl.common.exception.BizExceptionEnum;
import com.evl.common.exception.BussinessException;
import com.evl.common.persistence.dao.VehicleScheduleMapper;
import com.evl.common.persistence.model.VehicleSchedule;
import com.evl.core.util.ToolUtil;
import com.evl.core.vo.BaseResultVo;
import com.evl.core.vo.JsonReturnHelper;
import com.evl.vehicleSchedule.dao.VehicleScheduleDao;
import com.evl.vehicleSchedule.service.IVehicleScheduleService;
import com.evl.vehicleSchedule.vo.ScheduleVo;
import com.evl.vehicleSchedule.warpper.VehicleScheduleWarpper;

/**
 * 调度管理控制器
 *
 * @author 
 * @Date 2017-06-15 16:40:22
 */
@Controller
@RequestMapping("/vehicleSchedule")
public class VehicleScheduleController extends BaseController {

    private String PREFIX = "/vehicleSchedule/";

    @Resource
    VehicleScheduleMapper vehicleScheduleMapper;
    
    @Resource
    VehicleScheduleDao vehicleScheduleDao;
    
    @Resource
    IVehicleScheduleService vehicleScheduleService;
    
    /**
     * 跳转到调度管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "vehicleSchedule.html";
    }

    /**
     * 跳转到添加调度管理
     */
    @RequestMapping("/vehicleSchedule_add")
    public String vehicleScheduleAdd() {
        return PREFIX + "vehicleSchedule_add.html";
    }

    /**
     * 跳转到修改调度管理
     */
    @RequestMapping("/vehicleSchedule_update/{vehicleScheduleId}")
    public String vehicleScheduleUpdate(@PathVariable Integer vehicleScheduleId, Model model) {
    	VehicleSchedule vehicleSchedule = this.vehicleScheduleMapper.selectById(vehicleScheduleId);
    	model.addAttribute(vehicleSchedule);
        return PREFIX + "vehicleSchedule_edit.html";
    }
    
    /**
     * 跳转到调度管理详情
     */
    @RequestMapping("/vehicleSchedule_detail/{vehicleScheduleId}")
    public String vehicleScheduleDetail(@PathVariable Integer vehicleScheduleId, Model model) {
    	
    	ScheduleVo scheduleVo = null;
		try {
			scheduleVo = vehicleScheduleService.findScheduleDetail(vehicleScheduleId);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	model.addAttribute(scheduleVo);
    	
        return PREFIX + "vehicleSchedule_detail.html";
    }


    /**
     * 获取调度管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String vehicleNo) {
    	
    	List<Map<String, Object>> result = vehicleScheduleDao.selectVehicleSchedules(super.getPara("vehicleNo"));
   	 
   	 	return new VehicleScheduleWarpper(result).warp();
    }

    /**
     * 新增调度管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ScheduleVo scheduleVo) {
    	Map<String,Object> resultMap = new HashMap<String, Object>();
    	
    	if(StringUtils.isEmpty( scheduleVo.getBeginDate() )){
    		resultMap.put("result", "fail");
    		resultMap.put("msg", "开始日期不能为空");
    		return resultMap;
    	}
    	
    	if(StringUtils.isEmpty( scheduleVo.getEndDate() )){
    		resultMap.put("result", "fail");
    		resultMap.put("msg", "结束日期不能为空");
    		return resultMap;
    	}
    	
    	if(StringUtils.isEmpty( scheduleVo.getVehicleIds()) || scheduleVo.getVehicleIds().equals("[]")){
    		resultMap.put("result", "fail");
    		resultMap.put("msg", "调度车次不能为空");
    		return resultMap;
    	}
    	
    	try {
			vehicleScheduleService.insertVehicleSchedule(scheduleVo);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
		return resultMap;
    }

    /**
     * 删除调度管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public BaseResultVo delete(@RequestParam Integer vehicleScheduleId) {
    	Map<String,Object> resultMap= vehicleScheduleService.deleteVehicleSchedule(vehicleScheduleId);
    	return JsonReturnHelper.getSuccessWithData(resultMap);
    }


    /**
     * 修改调度管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(VehicleSchedule vehicleSchedule) {
    	if (ToolUtil.isEmpty(vehicleSchedule) || vehicleSchedule.getId() == null) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
    	
    	return this.vehicleScheduleMapper.updateById(vehicleSchedule);
    }

    /**
     * 调度管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(@RequestParam Integer vehicleScheduleId) {
        return this.vehicleScheduleMapper.selectById(vehicleScheduleId);
    }
    
    /**
     * 查看余票信息
     */
    @RequestMapping(value = "/findTicketRemainder")
    @ResponseBody
    public Object findTicketRemainder(ScheduleVo scheduleVo){
    	
    	
    	
    	return null;
    }
}
